/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.his.store.service.impl;

import me.zhengjie.modules.his.domain.Drugstore;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.his.store.repository.DrugstoreRepository;
import me.zhengjie.modules.his.store.service.DrugstoreService;
import me.zhengjie.modules.his.store.service.dto.DrugstoreDto;
import me.zhengjie.modules.his.store.service.dto.DrugstoreQueryCriteria;
import me.zhengjie.modules.his.store.service.mapstruct.DrugstoreMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author his
* @date 2022-11-11
**/
@Service
@RequiredArgsConstructor
public class DrugstoreServiceImpl implements DrugstoreService {

    private final DrugstoreRepository drugstoreRepository;
    private final DrugstoreMapper drugstoreMapper;

    @Override
    public Map<String,Object> queryAll(DrugstoreQueryCriteria criteria, Pageable pageable){
        Page<Drugstore> page = drugstoreRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(drugstoreMapper::toDto));
    }

    @Override
    public List<DrugstoreDto> queryAll(DrugstoreQueryCriteria criteria){
        return drugstoreMapper.toDto(drugstoreRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DrugstoreDto findById(Integer drugStoreId) {
        Drugstore drugstore = drugstoreRepository.findById(drugStoreId).orElseGet(Drugstore::new);
        ValidationUtil.isNull(drugstore.getDrugStoreId(),"Drugstore","drugStoreId",drugStoreId);
        return drugstoreMapper.toDto(drugstore);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DrugstoreDto create(Drugstore resources) {
        return drugstoreMapper.toDto(drugstoreRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Drugstore resources) {
        Drugstore drugstore = drugstoreRepository.findById(resources.getDrugStoreId()).orElseGet(Drugstore::new);
        ValidationUtil.isNull( drugstore.getDrugStoreId(),"Drugstore","id",resources.getDrugStoreId());
        drugstore.copy(resources);
        drugstoreRepository.save(drugstore);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer drugStoreId : ids) {
            drugstoreRepository.deleteById(drugStoreId);
        }
    }

    @Override
    public void download(List<DrugstoreDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DrugstoreDto drugstore : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("药品名称", drugstore.getDrugStoreName());
            map.put("供货单位", drugstore.getSupplierId());
            map.put("经办人", drugstore.getSkullId());
            map.put("计量单位", drugstore.getUnit());
            map.put("批发价", drugstore.getTradePrice());
            map.put("售价", drugstore.getSellingPrice());
            map.put("产地", drugstore.getArea());
            map.put("类型", drugstore.getType());
            map.put("生产日期", drugstore.getProduceDate());
            map.put("有效期", drugstore.getValIdDate());
            map.put("数量", drugstore.getDrugstoreNum());
            map.put("批号", drugstore.getBatch());
            map.put("创建者", drugstore.getCreateBy());
            map.put("更新者", drugstore.getUpdateBy());
            map.put("创建日期", drugstore.getCreateTime());
            map.put("更新时间", drugstore.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}